package com.zx.dsptuneup_TriMic.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.gyf.barlibrary.ImmersionBar;
import com.zx.dsptuneup_TriMic.Adapter.MatrixOutputAdapter;
import com.zx.dsptuneup_TriMic.Adapter.StrategyAdapter;
import com.zx.dsptuneup_TriMic.Base.BaseActivity;
import com.zx.dsptuneup_TriMic.Data_Bean.DevicesDetailInfoBean;
import com.zx.dsptuneup_TriMic.Data_Bean.MatrixVo;
import com.zx.dsptuneup_TriMic.Data_Bean.Strategy;
import com.zx.dsptuneup_TriMic.R;
import com.zx.dsptuneup_TriMic.Tools.Constant;
import com.zx.dsptuneup_TriMic.bean.BasicResponse;
import com.zx.dsptuneup_TriMic.network.DefaultObserver;
import com.zx.dsptuneup_TriMic.network.NetWorkManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class StrategyActivity extends BaseActivity {
    @BindView(R.id.list_view)
    RecyclerView list_view;

    @BindView(R.id.ic_nav)
    ImageView ic_nav;
    private List<Strategy> Strategylist = new ArrayList<>();
    private StrategyAdapter strategyAdapter;

    private int groupId = -1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_strategy);


        Intent intent = getIntent();
        groupId = intent.getIntExtra(Constant.GroupID,0);
        initUI();
        initData();
    }


    protected void initUI() {
        ImmersionBar.with(this)
                .transparentBar()
                .statusBarColor(R.color.home_bg)
                .statusBarDarkFont(false)
                .init();
        ic_nav.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        strategyAdapter = new StrategyAdapter(this,Strategylist);
        strategyAdapter.setOnDbChangeListener(new StrategyAdapter.OnDbChangeListener() {
            @Override
            public void DbChange(int position) {
                Strategy strategy = Strategylist.get(position);

                HashMap map = new HashMap();
                map.put("id",strategy.getId());
                map.put("group",groupId);
                NetWorkManager.getInstance().getNetWorkApiService().startStrategy(map).compose(StrategyActivity.this.<BasicResponse<String>>bindToLifecycle())
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new DefaultObserver<BasicResponse<String>>() {
                            @Override
                            protected void onSuccess(BasicResponse<String> response) {
                                if (response != null) {

                                }
                            }
                            @Override
                            protected void onFail(BasicResponse<String> response) {
                                super.onFail(response);

                            }
                            @Override
                            public void onError(@NonNull Throwable e) {
                                super.onError(e);
                            }
                        });
            }
        });
        list_view.setLayoutManager(new LinearLayoutManager(this));
        list_view.setAdapter(strategyAdapter);


    }
    private void initData() {
        NetWorkManager.getInstance().getNetWorkApiService().getStrategy().compose(StrategyActivity.this.<BasicResponse<List<Strategy>>>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DefaultObserver<BasicResponse<List<Strategy>>>() {
                    @Override
                    protected void onSuccess(BasicResponse<List<Strategy>> response) {
                        if (response != null) {
                            Strategylist = response.getData();
                            strategyAdapter.setGridViewBeanList(Strategylist);
                        }
                    }
                    @Override
                    protected void onFail(BasicResponse<List<Strategy>> response) {
                        super.onFail(response);

                    }
                    @Override
                    public void onError(@NonNull Throwable e) {
                        super.onError(e);
                    }
                });
    }
}
